import Vue from 'vue' //导入VueJS语法
import VueRouter from 'vue-router' //导入vue-router路由
//注册组件(安装)
Vue.use(VueRouter);
//设置路由
const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () =>
      import ( /* webpackChunkName:'login_home_welcome' */ '../components/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: () =>
      import ( /* webpackChunkName:'login_home_welcome' */ '../components/Home.vue'),
    children: [{
        path: '/welcome',
        name: 'Welcome',
        component: () =>
          import ( /* webpackChunkName:'login_home_welcome' */ '../components/Welcome.vue')
      },
      {
        path: '/users',
        name: 'Users',
        component: () =>
          import ( /* webpackChunkName:'Users_Rights_Roles' */ '../components/user/Users.vue')
      },
      {
        path: '/rights',
        name: 'Rights',
        component: () =>
          import ( /* webpackChunkName:'Users_Rights_Roles' */ '../components/power/Rights.vue')
      },
      {
        path: '/roles',
        name: 'Roles',
        component: () =>
          import ( /* webpackChunkName:'Users_Rights_Roles' */ '../components/power/Roles.vue')
      }, {
        path: '/categories',
        name: 'Cate',
        component: () =>
          import ( /* webpackChunkName:'Cate_Params' */ '../components/goods/Cate.vue')
      },
      {
        path: '/params',
        name: 'Params',
        component: () =>
          import ( /* webpackChunkName:'Cate_Params' */ '../components/goods/Params.vue')
      }, {
        path: '/goods',
        name: 'Goods',
        component: () =>
          import ( /* webpackChunkName:'Goods_Add' */ '../components/goods/List.vue')
      },
      {
        path: '/goods/add',
        name: 'Add',
        component: () =>
          import ( /* webpackChunkName:'Goods_Add' */ '../components/goods/Add.vue')
      },
      {
        path: '/orders',
        name: 'Order',
        component: () =>
          import ( /* webpackChunkName:'Order_Report' */ '../components/order/Order.vue')
      },
      {
        path: '/reports',
        name: 'Report',
        component: () =>
          import ( /* webpackChunkName:'Order_Report' */ '../components/report/Report.vue')
      },
    ]
  }
];
//实例化vue-router路由
const router = new VueRouter({
  routes
});
//挂载路由导航守卫(to为用户要访问的页面from为用户是从哪个路径跳转的next函数是你最终让用户去的)
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next(); //用户访问的是登录页则直接放行
  //获取用户登录的token有则跳转没有则继续呆在登录页
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login'); //不存在token则未登录成功就继续留在login
  next() //存在则跳转指定页面
});

//默认导出
export default router